package com.zh.edu.info.manager.dao;

import com.zh.edu.info.manager.domain.Student;
import com.zh.edu.info.manager.domain.Teacher;

import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao{
    private static ArrayList<Student> arr = new ArrayList<>();

    static {
        Student s1 = new Student("1", "小张", 25, "1997");
        Student s2 = new Student("2", "小王", 23, "1999");

        arr.add(s1);
        arr.add(s2);
    }

    @Override
    public boolean addStudent(Student student) {
        return arr.add(student);
    }

    @Override
    public Student[] findAll() {
        Student[] arr = new Student[this.arr.size()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = this.arr.get(i);
        }
        return arr;
    }

    @Override
    public void setStudent(String id, Student student) {
        int index = getIndex(id);
        arr.set(index, student);
    }

    @Override
    public void selectStudent(String id) {
        int index = getIndex(id);
        arr.remove(index);
    }

    @Override
    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < arr.size(); i++) {
            if (arr.get(i).getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

}
